class Solution(object):
    def numMovesStonesII(self, stones):
        stones.sort()
        n = len(stones)
        if stones[-1] - stones[0] == n - 1:
            return [0, 0]
        max_val = max(stones[n - 2] - stones[0] - n + 2, stones[n - 1] - stones[1] - n + 2)
        min_val = float('inf')
        left = 0
        right = 0
        if stones[n - 2] - stones[0] == n - 2 or stones[n - 1] - stones[1] == n - 2:
            min_val = min(2, stones[n - 1] - stones[0] - n + 1)
        else:
            while right < n:
                while stones[right] - stones[left] > n - 1:
                    left += 1
                min_val = min(min_val, n - (right - left + 1))
                right += 1
        return [min_val, max_val]


data = Solution()
stones = [7, 4, 9]
print(data.numMovesStonesII(stones))
